<p>This rule raises an issue when Angular lifecycle methods are implemented in classes where they won’t be called by the framework.</p>
<h2>Why is this an issue?</h2>
<p>Angular lifecycle methods are only called by the framework in specific contexts. Each decorator type (@Component, @Directive, @Injectable,
@NgModule, @Pipe) supports different lifecycle methods.</p>
<p>Using lifecycle methods in the wrong context creates several problems:</p>
<ul>
  <li> The methods will never be called by Angular, making the code ineffective </li>
  <li> It misleads other developers about when the code will execute </li>
  <li> It can indicate a misunderstanding of Angular’s architecture </li>
  <li> It adds unnecessary code that serves no purpose </li>
</ul>
<p>For example, view-related lifecycle methods like <code>ngAfterViewInit</code> only work in Components and Directives that have templates. Using
them in Services or Pipes is meaningless since these don’t have views.</p>
<h3>What is the potential impact?</h3>
<p>The code will not behave as expected since the lifecycle methods won’t be called. This can lead to initialization logic not running, cleanup not
happening, or change detection not working properly. It may also confuse team members about the application’s behavior.</p>
<h3>How to fix in Angular?</h3>
<p>Remove lifecycle methods that are not supported in the current context, or move the logic to an appropriate lifecycle method or initialization
pattern.</p>
<h4>Non-compliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
@Injectable()
class UserService {
  ngOnInit() { // Noncompliant - Services don't support ngOnInit
    this.loadUsers();
  }

  ngAfterViewInit() { // Noncompliant - Services don't have views
    console.log('View initialized');
  }
}
</pre>
<h4>Compliant code example</h4>
<pre data-diff-id="1" data-diff-type="compliant">
@Injectable()
class UserService {
  constructor() {
    this.loadUsers(); // Use constructor for initialization
  }

  ngOnDestroy() { // Only ngOnDestroy is supported in services
    this.cleanup();
  }
}
</pre>
<p>Use ngDoBootstrap only in NgModule classes, not in Components or other contexts.</p>
<h4>Non-compliant code example</h4>
<pre data-diff-id="2" data-diff-type="noncompliant">
@Component({
  selector: 'app-root',
  template: '&lt;div&gt;App&lt;/div&gt;'
})
class AppComponent {
  ngDoBootstrap() { // Noncompliant - Components don't support ngDoBootstrap
    console.log('Bootstrapping');
  }
}
</pre>
<h4>Compliant code example</h4>
<pre data-diff-id="2" data-diff-type="compliant">
@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule]
})
class AppModule {
  ngDoBootstrap() { // Correct - NgModules support ngDoBootstrap
    console.log('Bootstrapping');
  }
}
</pre>
<h3>Documentation</h3>
<ul>
  <li> <a href="https://github.com/angular-eslint/angular-eslint">Angular ESLint</a> - Rule <a
  href="https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/contextual-lifecycle.md">contextual-lifecycle</a>
  </li>
  <li> Angular Lifecycle Hooks - <a href="https://angular.dev/guide/components/lifecycle">Official Angular documentation on lifecycle hooks and their
  usage contexts</a> </li>
  <li> Angular Services and Dependency Injection - <a href="https://angular.dev/guide/di">Guide on Angular services and their lifecycle</a> </li>
</ul>
